From 0b7496558d0d4a3f5e2d31b4cbbc9ce20e1d06fc Mon Sep 17 00:00:00 2001 From: Carlos Garnacho Date: Wed, 19 Jan 2011 04:12:08 +0100 Subject: [PATCH] Make GtkCssProvider deal with widget types not being in plain CamelCase MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Fixes bug #Bug 639754, reported by Kjell Ahlstedt. gtkmm doesn't use plain CamelCase for its widget type names, so in order to distinguish widget type names from regions in the CSS parser, the following checks are now done: * if it contains an uppercase letter -> widget class (that should also work for gtkmm) * if it's a string compound by lowercase letters and '-' -> it's a region, checks have been added in gtk_style_context_add_region() and gtk_widget_path_iter_add_region() to ensure this. --- gtk/gtkcssprovider.c | 26 ++++++++++++++++++++++---- gtk/gtkstylecontext.c | 24 ++++++++++++++++++++++++ gtk/gtkstylecontext.h | 2 ++ gtk/gtkwidgetpath.c | 4 ++++ 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/gtk/gtkcssprovider.c b/gtk/gtkcssprovider.c index 89d549226b..ea4530e9b4 100644 --- a/gtk/gtkcssprovider.c +++ b/gtk/gtkcssprovider.c @@ -1711,6 +1711,25 @@ parse_classes (SelectorPath *path, selector_path_prepend_class (path, str); } +static gboolean +is_widget_class_name (const gchar *str) +{ + /* Do a pretty lax check here, not all + * widget class names contain only CamelCase + * (gtkmm widgets don't), but at least part of + * the name will be CamelCase, so check for + * the first uppercase char */ + while (*str) + { + if (g_ascii_isupper (*str)) + return TRUE; + + str++; + } + + return FALSE; +} + static GTokenType parse_selector (GtkCssProvider *css_provider, GScanner *scanner, @@ -1761,7 +1780,7 @@ parse_selector (GtkCssProvider *css_provider, parse_classes (path, pos + 1); } } - else if (g_ascii_isupper (scanner->value.v_identifier[0])) + else if (is_widget_class_name (scanner->value.v_identifier)) { gchar *pos; @@ -1802,7 +1821,7 @@ parse_selector (GtkCssProvider *css_provider, else selector_path_prepend_type (path, scanner->value.v_identifier); } - else if (g_ascii_islower (scanner->value.v_identifier[0])) + else if (_gtk_style_context_check_region_name (scanner->value.v_identifier)) { GtkRegionFlags flags = 0; gchar *region_name; @@ -3313,8 +3332,7 @@ parse_rule (GtkCssProvider *css_provider, return G_TOKEN_IDENTIFIER; } } - else if (prop[0] == '-' && - g_ascii_isupper (prop[1])) + else if (prop[0] == '-') { GValue *val; diff --git a/gtk/gtkstylecontext.c b/gtk/gtkstylecontext.c index e36bef4eb7..0f30fcd3a1 100644 --- a/gtk/gtkstylecontext.c +++ b/gtk/gtkstylecontext.c @@ -2069,6 +2069,26 @@ gtk_style_context_list_regions (GtkStyleContext *context) return classes; } +gboolean +_gtk_style_context_check_region_name (const gchar *str) +{ + g_return_val_if_fail (str != NULL, FALSE); + + if (!g_ascii_islower (str[0])) + return FALSE; + + while (*str) + { + if (*str != '-' && + !g_ascii_islower (*str)) + return FALSE; + + str++; + } + + return TRUE; +} + /** * gtk_style_context_add_region: * @context: a #GtkStyleContext @@ -2095,6 +2115,9 @@ gtk_style_context_list_regions (GtkStyleContext *context) * * would apply to even and odd rows, respectively. * + * Region names must only contain lowercase letters + * and '-', starting always with a lowercase letter. + * * Since: 3.0 **/ void @@ -2109,6 +2132,7 @@ gtk_style_context_add_region (GtkStyleContext *context, g_return_if_fail (GTK_IS_STYLE_CONTEXT (context)); g_return_if_fail (region_name != NULL); + g_return_if_fail (_gtk_style_context_check_region_name (region_name)); priv = context->priv; region_quark = g_quark_from_string (region_name); diff --git a/gtk/gtkstylecontext.h b/gtk/gtkstylecontext.h index a2ab473546..42eed4beee 100644 --- a/gtk/gtkstylecontext.h +++ b/gtk/gtkstylecontext.h @@ -595,6 +595,8 @@ const GValue * _gtk_style_context_peek_style_property (GtkStyleContext *context, void _gtk_style_context_invalidate_animation_areas (GtkStyleContext *context); void _gtk_style_context_coalesce_animation_areas (GtkStyleContext *context, GtkWidget *widget); +gboolean _gtk_style_context_check_region_name (const gchar *str); + void gtk_style_context_invalidate (GtkStyleContext *context); void gtk_style_context_reset_widgets (GdkScreen *screen); diff --git a/gtk/gtkwidgetpath.c b/gtk/gtkwidgetpath.c index 6636abdd14..f0ae30ea14 100644 --- a/gtk/gtkwidgetpath.c +++ b/gtk/gtkwidgetpath.c @@ -730,6 +730,9 @@ gtk_widget_path_iter_has_class (const GtkWidgetPath *path, * the hierarchy defined in @path. See * gtk_style_context_add_region(). * + * Region names must only contain lowercase letters + * and '-', starting always with a lowercase letter. + * * Since: 3.0 **/ void @@ -744,6 +747,7 @@ gtk_widget_path_iter_add_region (GtkWidgetPath *path, g_return_if_fail (path != NULL); g_return_if_fail (path->elems->len != 0); g_return_if_fail (name != NULL); + g_return_if_fail (_gtk_style_context_check_region_name (name)); if (pos < 0 || pos > path->elems->len) pos = path->elems->len - 1; -- 2.30.2